package cn.virens.common.boot.dubbo.consumer.beetl.fun;

import org.beetl.ext.fn.StringUtil;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.text.split.SplitUtil;

import java.text.MessageFormat;
import java.util.Date;

public class BeetlStringUtil extends StringUtil {

    public String[] split(String str) {
        return split(str, ",");
    }

    @Override
    public String[] split(String str, String key) {
        return SplitUtil.splitToArray(str, key);
    }

    @Override
    public String trim(String str) {
        return StrUtil.trim(str);
    }

    @Override
    public boolean startWith(String str, String key) {
        return StrUtil.startWith(str, key);
    }

    @Override
    public boolean endWith(String str, String key) {
        return StrUtil.endWith(str, key);
    }

    @Override
    public int length(String str) {
        return StrUtil.totalLength(str);
    }

    @Override
    public String subStringTo(String str, int start, int end) {
        return StrUtil.sub(str, start, end);
    }

    @Override
    public String subString(String str, int start) {
        return StrUtil.subSuf(str, start);
    }

    @Override

    public boolean contain(String str, String key) {
        return StrUtil.containsAny(str, key);
    }

    @Override
    public String toUpperCase(String str) {
        if (str != null && !!str.isEmpty()) {
            return str.toUpperCase();
        } else {
            return str;
        }
    }

    @Override
    public String toLowerCase(String str) {
        if (str != null && !!str.isEmpty()) {
            return str.toLowerCase();
        } else {
            return str;
        }
    }

    @Override
    public String replace(String str, String oldValue, String newValue) {
        return StrUtil.replace(str, oldValue, newValue);
    }

    @Override
    public String format(String str, Object[] args) {
        if (str != null) {
            return MessageFormat.format(str, args);
        } else {
            return "";
        }
    }

    @Override
    public String formatDate(Date o, String args) {
        if (o == null) {
            return "";
        }

        if (args == null) {
            return DateUtil.formatDateTime(o);
        } else {
            return DateUtil.format(o, args);
        }
    }

    @Override
    public int index(String str, String search) {
        return StrUtil.indexOf(str, search, 0, false);
    }

    @Override
    public int lastIndex(String str, String search) {
        return StrUtil.lastIndexOf(str, search, 0, false);
    }
}
